﻿Imports System.Data.OleDb
Public Class updateBuild
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            Dim sql As String = "SELECT * FROM Profiles WHERE userName=@f8"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("connstr").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f8", Membership.GetUser(User.Identity.Name).ProviderUserKey)


            Dim odr = cmd.ExecuteReader

            odr.Read()


            If Not IsDBNull(odr("FirstName")) Then tb_fname.Text = odr("FirstName")
            If Not IsDBNull(odr("LastName")) Then tb_lname.Text = odr("LastName")
            If Not IsDBNull(odr("Gender")) Then ddl_gender.SelectedValue = odr("Gender")
            If Not IsDBNull(odr("DoB")) Then tb_dob.Text = odr("DoB")
            If Not IsDBNull(odr("PreferredOS")) Then ddl_os.SelectedValue = odr("PreferredOS")
            If Not IsDBNull(odr("PreferredCPU")) Then ddl_cpu.SelectedValue = odr("PreferredCPU")
            If Not IsDBNull(odr("PreferredScreenResolution")) Then ddl_resolution.SelectedValue = ("PreferredScreenResolution")
        End If

    End Sub

    Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Submit.Click


        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(fp_BuildPicture.FileName)
        If fp_BuildPicture.HasFile Then
            fp_BuildPicture.SaveAs(Server.MapPath("Pictures") & "/" & newFileName)
        End If


        Dim sql As String = "INSERT INTO Builds (BuildName,CPU,Motherboard,RAM,Graphics,HardDrive,Power,Casing,Audio,Mouse,Keyboard,Monitor,TotalCostToBuild,BuildPicture,AddedBy,DateAdded,Description) VALUES (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8,@f9,@f10,@f11,@f12,@f13,@f14,@f15,@f16,@f17)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("connstr").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)




        cmd.Parameters.AddWithValue("@f1", tb_BuildName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_CPU.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Motherboard.Text)
        cmd.Parameters.AddWithValue("@f4", ddl_ram.SelectedValue)
        cmd.Parameters.AddWithValue("@f5", tb_Graphics.Text)
        cmd.Parameters.AddWithValue("@f6", ddl_hardDrive.SelectedValue)
        cmd.Parameters.AddWithValue("@f7", ddl_power.SelectedValue)
        cmd.Parameters.AddWithValue("@f8", tb_Casing.Text)
        cmd.Parameters.AddWithValue("@f9", tb_Audio.Text)
        cmd.Parameters.AddWithValue("@f10", tb_Mouse.Text)
        cmd.Parameters.AddWithValue("@f11", tb_Keyboard.Text)
        cmd.Parameters.AddWithValue("@f12", tb_Monitor.Text)
        cmd.Parameters.AddWithValue("@f13", tb_CostToBuild.Text)
        cmd.Parameters.AddWithValue("@f14", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f15", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.Parameters.AddWithValue("@f16", DateTime.Now.Date)
        cmd.Parameters.AddWithValue("@f17", tb_description.Text)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("Default.aspx")
    End Sub


    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

End Class